﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using PPI.Client;
using PPI.Client.Presenter;
using PPI.Client.Utils;
using PPI.Entities;

namespace PPI
{
    public partial class frmDeployDistributionDatabase : frmDeployBase
    {
        private List<EVendor> m_ListVendors;
        private List<EUtility> m_ListUtility;
        private List<EVendor_Contact> m_ListContacts;
        private List<EVendor_Contact> m_ListSelectedContacts;

        #region - Constructor -

        public frmDeployDistributionDatabase()
        {
            InitializeComponent();

            MenuMain.Visible = false;
            FormSize = FormSizes.Medium;
        }

        #endregion

        #region - Event handlers -

        private void frmDeployDistributionDatabase_Load(object sender, EventArgs e)
        {
            try
            {
                m_Presenter = new DeployDistributionDatabasePresenter(this);

                GetCacheData();
                Fill_Vendors();
                Fill_Utilities();
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        #endregion

        #region - frmDeployBase Overrides -

        /// <summary>
        /// Called after the task has finished
        /// </summary>
        protected override void DoNewDeploy()
        {
            if (MessageBox.Show("Would you like to do another 'distribution' deployment?", "New Deploy", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                Close();
        }

        /// <summary>
        /// Executes the deploy actions
        /// </summary>
        protected override void DoDeploy()
        {
            try
            {
                if (ValidateAll())
                {
                    string sourceDatabase;

                    try
                    {
                        DeployDistributionDatabasePresenter logicDatabase =
                            (DeployDistributionDatabasePresenter) m_Presenter;
                        //sourceDatabase = logicDatabase.DeployDistribution(rdbGPS.Checked ? 0 : 1);
                        int vendorId = Convert.ToInt32(((ListBoxItem) cmbVendor.SelectedItem).Value);
                        int vendorContactId = Convert.ToInt32(((ListBoxItem) cmbContact.SelectedItem).Value);
                        int vendorProjectId = Convert.ToInt32(((ListBoxItem)cmbProject.SelectedItem).Value);
                        int utilityId = Convert.ToInt32(((ListBoxItem)cmbUtility.SelectedItem).Value);
                        string vendorName = ((ListBoxItem) cmbVendor.SelectedItem).Text;
                        string vendorContactName = ((ListBoxItem) cmbContact.SelectedItem).Text;
                        string utilityName = ((ListBoxItem)cmbUtility.SelectedItem).Text;

                        sourceDatabase = logicDatabase.DeployDistribution(vendorId, vendorName, vendorContactId,
                                                                          vendorContactName, rdbGPS.Checked ? 0 : 1, vendorProjectId, utilityId, utilityName);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }

                    DirectoryInfo _DestinationFolder = SelectDestinationFolder();

                    if (_DestinationFolder == null)
                        return;

                    m_Presenter.DownloadFile(sourceDatabase,
                                             _DestinationFolder.FullName + "\\" + Constants.K_LOCALDATABASEDISTRIBUTION);
                    m_Presenter.CopyLocalFilesToDestination(_DestinationFolder.FullName);
                }
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        #endregion
        
        private void cmbVendor_SelectedIndexChanged(object sender, EventArgs e)
        {
            Fill_Contacts();
            Fill_Projects();
            ActivateDeploy();
        }

        private void Fill_Utilities()
        {
            DropdownHelper.FillDropDown(cmbUtility, m_ListUtility, typeof(EUtility), "Name", "Id", "Select an option...");
        }

        private void Fill_Vendors()
        {
            DropdownHelper.FillDropDown(cmbVendor, m_ListVendors, typeof(EVendor), "Name", "Id", "Select an option...");
        }

        private void Fill_Projects()
        {
            if (cmbVendor.SelectedIndex > 0)
            {
                Int32 selectedVendorId = Convert.ToInt32(((ListBoxItem)cmbVendor.SelectedItem).Value);

                var vendor = m_ListVendors.Find(x => x.Id == selectedVendorId);

                if (vendor != null)
                    DropdownHelper.FillDropDown(cmbProject, vendor.Projects, typeof(EVendor_Project), "Name", "Id", "Select an option...");
            }
        }

        private void Fill_Contacts()
        {
            if (cmbVendor.SelectedIndex > 0) 
            {
                Int32 selectedVendorId = Convert.ToInt32(((ListBoxItem)cmbVendor.SelectedItem).Value);

                m_ListSelectedContacts = m_ListContacts.FindAll(contact => contact.Vendor.Id == selectedVendorId);
                DropdownHelper.FillDropDown(cmbContact, m_ListSelectedContacts, typeof(EVendor_Contact), "Name", "Id", "Select an option...");
            }
        }

        private void GetCacheData()
        {
            m_ListVendors = LCacheDataSingleton.Instance.Vendor;
            m_ListContacts = LCacheDataSingleton.Instance.Contacts;
            m_ListUtility = LCacheDataSingleton.Instance.Utilities;
        }

        private void cmbContact_SelectedIndexChanged(object sender, EventArgs e)
        {
            ActivateDeploy();
        }

        private void rdbInspection_CheckedChanged(object sender, EventArgs e)
        {
            pnlVendor.Visible = rdbInspection.Checked;
        }

        private void cmbProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            ActivateDeploy();
        }
    }
}
